package com.blue.base.local.test.es;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author liulei44
 * @date 2022/5/8 10:42 PM
 */
@Slf4j
@RestController
@RequestMapping("es/")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ESController {

    private final RestHighLevelClient restHighLevelClient;

    @GetMapping("add/index")
    public Object addIndex(@RequestParam String index) throws IOException {
        //1.使用client获取操作索引的对象
        IndicesClient indicesClient = restHighLevelClient.indices();
        //2.具体操作，获取返回值
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
        //3.根据返回值判断结果
        return indicesClient.create(createIndexRequest, RequestOptions.DEFAULT);
    }
}
